package com.wlhse.controller;

import com.wlhse.dao.CheckRecordDao;
import com.wlhse.dto.getDto.ChangQinEmployeeDto;
import com.wlhse.dto.getDto.EmployeeDto;
import com.wlhse.dto.inDto.*;
import com.wlhse.dto.outDto.SuperVisionGroupMemberOutDto;
import com.wlhse.entity.SuperVisionGroupMemberPojo;
import com.wlhse.service.CheckRecordService;
import com.wlhse.util.GetCurrentUserIdUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController("CheckRecordController")
@RequestMapping("/api/v1")
public class CheckRecordController {

    @Resource
    private CheckRecordService checkRecordService;

    @Resource
    private GetCurrentUserIdUtil getCurrentUserIdUtil;

    @Resource
    private CheckRecordDao checkRecordDao;

    //按条件查询
    @RequestMapping(value = "/check_record", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String selectCheckRecordByCondition(@ModelAttribute CheckRecordInDto checkRecordInDto) {
        return checkRecordService.queryCheckRecord(checkRecordInDto);
    }

    //按id查询
    @RequestMapping(value = "/check_record/{id}", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String selectCheckRecordByID(@PathVariable("id") int id) {
        System.out.println(id);
        return checkRecordService.queryCheckRecordID(id);
    }

    //问题查询模块--新查询条件
    @RequestMapping(value = "/query_check_record", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String queryCheckRecordByCondition(@ModelAttribute CheckRecordQueryInDto checkRecordQueryInDto) {
        return checkRecordService.queryCheckRecordByCondition(checkRecordQueryInDto);
    }

    //删除
    @RequestMapping(value = "/check_record/{id}", method = RequestMethod.DELETE, produces = "application/json; charset=utf-8")
    public String deleteCheckRecord(@PathVariable("id") int id) {
        return checkRecordService.deleteCheckRecord(id);
    }

    //更新验证数据
    @RequestMapping(value = "/check_record",method = RequestMethod.PUT,produces = "application/json; charset=utf-8")
    public String updateCheckRecord(@RequestBody(required = false) CheckRecordVerifyInDto checkRecordVerifyInDto){
        return checkRecordService.updateCheckRecord(checkRecordVerifyInDto);
    }

    //更新整改数据
    @RequestMapping(value = "/check_record/recitification",method = RequestMethod.PUT,produces = "application/json; charset=utf-8")
    public String updateCheckRecord(@RequestBody(required = false) CheckRecordReciInDto checkRecordReciInDto, HttpServletRequest request){
        System.out.println("更新整改数据");
        checkRecordReciInDto.setSupervisorID(getCurrentUserIdUtil.getUserId(request));
        return checkRecordService.updateCheckRecordReci(checkRecordReciInDto);
    }

    //我验证的问题
    @RequestMapping(value = "/my_check_record_verify", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String selectMyCheckRecordVerify(@ModelAttribute CheckRecordInDto checkRecordInDto, HttpServletRequest request) {
        checkRecordInDto.setVerifyPersonEmpID(getCurrentUserIdUtil.getUserId(request));
        System.out.println(getCurrentUserIdUtil.getUserId(request));
        return checkRecordService.queryCheckRecordMine(checkRecordInDto);
    }

    //按id查询我验证的问题
    @RequestMapping(value = "/check_record_verify/{id}", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String selectCheckRecordVerifyByID(@PathVariable("id") int id) {
        return checkRecordService.queryCheckRecordMineByID(id);
    }

    //我已整改的问题，包括未验证、已验证
    @RequestMapping(value = "/my_check_record_reci", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String  selectMyCheckRecordReci(@ModelAttribute CheckRecordInDto checkRecordInDto, HttpServletRequest request) {
        checkRecordInDto.setSupervisorID(getCurrentUserIdUtil.getUserId(request));
        return checkRecordService.queryCheckRecordMineReci(checkRecordInDto);
    }

    //长庆employee模糊查询
    @RequestMapping(value = "/changqin_employee", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String  selectEmployee(@ModelAttribute ChangQinEmployeeDto changQinEmployeeDto) {
        return checkRecordService.selectEmployee(changQinEmployeeDto);
    }

    //supervisiongroupmember查询
    @RequestMapping(value = "/supervision_member", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String  selectSuperVisionGroupMember(@ModelAttribute SuperVisionGroupMemberOutDto superVisionGroupMemberOutDto) {
        return checkRecordService.selectSuperVisionGroupMember(superVisionGroupMemberOutDto);
    }

    //当前登陆人暂存问题条件查询
    @RequestMapping(value = "/check_record_temporary", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String selectCheckRecordTemporarySave(@ModelAttribute CheckRecordTemporarySaveInDto checkRecordTemporarySaveInDto) {
        return checkRecordService.selectCheckRecordTemporarySave(checkRecordTemporarySaveInDto);
    }

    //checkrecord和整改记录一起查询
    //问题查询模块--新查询条件
    @RequestMapping(value = "/query_check_records", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String queryCheckRecordsByCondition(@ModelAttribute CheckRecordQueryInDto checkRecordQueryInDto) {
        return checkRecordService.queryCheckRecordsByCondition(checkRecordQueryInDto);
    }

    //checkrecord和整改记录一起查询--历史tree显示
    //问题查询模块--新查询条件
    //2008-CheckStaffID
    //2009-SuperVisorID
    @RequestMapping(value = "/query_check_tree", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String queryCheckRecordsTreeByCondition(@ModelAttribute CheckRecordQueryInDto checkRecordQueryInDto,HttpServletRequest request) {
        Integer id = getCurrentUserIdUtil.getUserId(request);
        //可能有多个权限
        List<String> codes = checkRecordDao.getRoleCodess(id);//进行排序
        //取权限最小的
        String code = codes.get(codes.size()-1);
        if (code.equals("2008")){
            checkRecordQueryInDto.setCheckStaffID(id);
        }else if (code.equals("2009")){
            checkRecordQueryInDto.setSupervisorID(id);
        }
        checkRecordQueryInDto.setRoleCode(code);
        System.out.println("最后的角色"+checkRecordQueryInDto.getRoleCode());
        return checkRecordService.queryCheckRecordsTreeByCondition(checkRecordQueryInDto,id);
    }


    //我待整改的问题
    @RequestMapping(value = "/check_record_reci/will", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String getReciCheckRecordMine(@ModelAttribute CheckRecordInDto checkRecordInDto, HttpServletRequest request) {
        checkRecordInDto.setSupervisorID(getCurrentUserIdUtil.getUserId(request));
        System.out.println("id:"+checkRecordInDto.getSupervisorID());
        return checkRecordService.queryCheckRecord(checkRecordInDto);
    }

    //我待验证的问题,检查人是当前登陆人
    @RequestMapping(value = "/check_record_verify/will", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String getVerifyCheckRecordMine(@ModelAttribute CheckRecordInDto checkRecordInDto, HttpServletRequest request) {
        checkRecordInDto.setVerifyPersonEmpID(getCurrentUserIdUtil.getUserId(request));
        System.out.println(checkRecordInDto.getVerifyPersonEmpID());
        return checkRecordService.queryCheckRecord(checkRecordInDto);
    }

    //问题类型
    @RequestMapping(value = "/hazard_factors", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String getHazardFactors() {
        return checkRecordService.getHazardFactors();
    }

    //当前登陆人code
    //如果有多个权限，取最小的权限
    @RequestMapping(value = "/current_code_Headman", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String current_code_Headman(HttpServletRequest request) {
        Integer id = getCurrentUserIdUtil.getUserId(request);
        return checkRecordService.current_code_Headman(id);
    }

    //当前登陆人code
    //如果有多个权限，取最高的权限
    @RequestMapping(value = "/current_code_Admin", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String current_code_Admin(HttpServletRequest request) {
        Integer id = getCurrentUserIdUtil.getUserId(request);
        return checkRecordService.current_code_Admin(id);
    }

    //更新验证人
    @RequestMapping(value = "/check_record/verify",method = RequestMethod.PUT,produces = "application/json; charset=utf-8")
    public String updateVerifyPerson(@RequestBody(required = false) CheckRecordVerifyInDto2 checkRecordVerifyInDto){
        return checkRecordService.updateVerifyPerson(checkRecordVerifyInDto);
    }

}
